import VueRouter from 'vue-router'
import Home from "@/Home";
import Login from "@/Login";
import Vue from "vue";



Vue.use(VueRouter);

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
    },
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
      // 1.如果访问的是登录页面（无需权限），直接放行
  if (to.path === '/login') return next()
  // 2.如果访问的是有登录权限的页面，先要获取token
  const tokenStr =localStorage.getItem('token')
  // 2.1如果token为空，强制跳转到登录页面；否则，直接放行
  if (!tokenStr) return next('/login')
  next()

})
export default router